<template>
    <div style="width: 500px">
        <el-input
                placeholder="请输入部门名称搜索部门..."
                prefix-icon="el-icon-search"
                v-model="filterText">
        </el-input>

        <el-tree style="margin-top: 10px"
                 class="filter-tree"
                 :expand-on-click-node="false"
                 :data="deps"
                 :props="defaultProps"
                 :filter-node-method="filterNode"
                 ref="tree">
                 <span class="custom-tree-node" style="display: flex;justify-content: space-between;width: 100%"
                       slot-scope="{ node, data }">
                    <span>{{ data.name }}</span>
                    <span>
                      <el-button
                              type="primary"
                              size="mini"
                              class="depBtn"
                              @click="() => showAddDepView(data)">
                        添加部门
                      </el-button>
                      <el-button
                              type="danger"
                              size="mini"
                              class="depBtn"
                              @click="() => deleteDep(data)">
                        删除部门
                      </el-button>
                    </span>
                 </span>
        </el-tree>
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="20%">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag>上级部门</el-tag>
                        </td>
                        <td>{{pname}}</td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>部门名称</el-tag>
                        </td>
                        <td>
                            <el-input v-model="dep.name" placeholder="请输入部门名称..."></el-input>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="doAddDep">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "DepMana",
        data() {
            return {
                dialogVisible: false,
                filterText: '',
                deps: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                dep: {
                    name: '',
                    parentId: -1
                },
                pname: '',
            }
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        mounted() {
            this.initDeps();
        },
        methods: {
            showAddDepView(data) {
                this.pname = data.name;
                this.dep.parentId = data.id;
                this.dialogVisible = true;
            },
            deleteDep(data) {
                console.log(data);
            },
            initDeps() {
                this.getRequest("/system/basic/department/").then(resp => {
                    if (resp) {
                        this.deps = resp;
                    }
                })
            },
            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },
            doAddDep() {
                this.putRequest("/system/basic/department/", this.dep).then(resp => {
                    if (resp) {
                        //递归动态添加部门
                        this.addDep2Deps(this.deps, resp.data);
                        this.dialogVisible = false;
                        //初始化变量
                        this.initDep();
                    }
                })
            },
            addDep2Deps(deps, dep) {
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                    if (d.id == dep.parentId) {
                        d.children = d.children.concat(dep);
                        if (d.children.length > 0) {
                            d.parent = true;
                        }
                        return;
                    } else {
                        this.addDep2Deps(d.children, dep);
                    }
                }
            },
            initDep() {
                this.dep = {
                    name: '',
                    parentId: -1
                }
                this.pname = '';
            },
            deleteDep(data) {
                if (data.parent) {
                    this.$message.error("父部门删除失败");
                } else {
                    this.$confirm('此操作将永久删除【' + data.name + '】部门, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest("/system/basic/department/" + data.id).then(resp => {
                            if (resp) {
                                this.removeDepFromDeps(null, this.deps, data.id);  //最开始是根部门，没有父部门
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            removeDepFromDeps(p, deps, id) {
                /*循环遍历部门树，通过后端传过来的id查找要删除的部门*/
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                    //到达当前要删除的id
                    if (d.id === id) {
                        deps.splice(i, 1);
                        /*删除了后如果原先是父部门的要判断低下是否还有子部门*/
                        if (deps.length == 0) {
                            p.parent = false;
                        }
                        //删除后终止操作
                        return;
                    } else {
                        this.removeDepFromDeps(d, d.children, id)
                    }
                }
            }
        }
    }
</script>

<style>
    .depBtn {
        padding: 5px;
    }

</style>